<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status']=array(
		'0'=>'待付款_NO pay',
		'1'=>'待发货_NO send',
		'2'=>'待收货_NO get',
		'3'=>'已完成_Payment complete',
		'4'=>'已关闭_exchange',
		'5'=>'申请退款',
		'6'=>'已退款',
		'7'=>'异常报备',
		'10'=>'拼单中',
		'11'=>'待售后接单',
		'12'=>'售后已接单',
		'13'=>'售后上门',
		'14'=>'售后异常',
		'15'=>'待售后',
		'-1'=>'已关闭'
);
$return['send_status']=$this->send_status;
$return['status2']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['ispay']=array('0'=>'未支付_No pay','1'=>'已支付_Is pay');
$shopset=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'ispay'=>1);
    $orderby='id desc';
	$total='';
	if(!empty($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(isset($_RQ['send_status'])){
		$params['send_status']=intval($_RQ['send_status']);
		$return['params']['send_status']=$_RQ['send_status'];
	}
	if(isset($_RQ['status'])){
		if($_RQ['status']==15){
			$params['status']=array(11,12,13,14,15,16);
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['params']['status']=$_RQ['status'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$tip='所有订单显示，列表商品只显示第一个！';
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['member']['nickname'] .=' (ID:'.$item['mid'].')';
		$goods=pdo_get('jy_weishop_order_goods',array('orderid'=>$item['id']),array('goodsid','total'));
		$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid']),array('title','image','unit'));
		$item['total']=$goods['total'];
		$item['title']=$goodsinfo['title'];
		$item['image']=$goodsinfo['image'];
		$item['unit']=$goodsinfo['unit'];
		if(!$item['merchid'] && $item['storeid']){
			$item['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$item['storeid']),'title');
		}
		if($item['merchid']){
			$item['storetitle']='商户';
		}
		$item['storetitle']=$item['storetitle']?:'平台自营';
		$item['expressid']=pdo_getcolumn('jy_weishop_order_express',array('orderid'=>$item['id']),'id');
		$item['isjy_shouhou']=$jy_shouhou;
		$address=$item['address']?json_decode($item['address'],true):$item['addressid'];
		if($address){
			$item['realname']=$address['name']?:$item['realname'];
			$item['address']=model('core/member')->get_address_str($address);
		}
		if($item['sendtype']==1 && $item['is_daifa']==1){
			$item['sendtype']='daifa';
		}
		$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		$item['isshlian']=0;
		if(strpos($item['api_data'],'isshlian') !== false){
			$item['isshlian']=1;
		}
		$item['api_data']=json_decode($item['api_data'],true);
		$item['esignok']=$item['api_data']['esignok']?1:0;
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理_Order list'),
		),
		'list'=>array('data'=>'list','tip'=>$tip,'edit'=>false,'del_one'=>false,'delall'=>false,'html_to'=>'list_html','list'=>array(
			array('name'=>'ordersn','title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'realname','title'=>'收件人_Realname','type'=>'string'),
			array('type'=>'string_more','title'=>'门店/商户_Store',
			'data'=>array(
				array('title'=>'门店：_Store：','name'=>'storetitle'),
				array('title'=>'商户：_merch：','name'=>'merchid','type'=>'select','options'=>'merchs')
				)
			),
			array('type'=>'img_string','title'=>'商品名称/数量_Goods','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'数量：_num：','name'=>'total','unit'=>'unit')
				)
			),
			array('type'=>'string_more','title'=>'金额/时间_Price',
			'data'=>array(
				array('title'=>'金额：_Price：','name'=>'price'),
				array('title'=>'时间：_Time：','name'=>'createtime')
				)
			),
			array('type'=>'string_more','title'=>'支付/状态_Is pay',
			'data'=>array(
				array('title'=>'','name'=>'ispay','type'=>'radio','options'=>'ispay'),
				array('title'=>'','name'=>'paytype','type'=>'radio','options'=>'paytype'),
				array('title'=>'','name'=>'esignok','type'=>'radio','options'=>'esignok')
				)
			),
		),
		'action'=>array(
			array('type'=>'href','action'=>'/order.detail/','title'=>'订单详情_Detail'),
			array('op'=>'uplian','title'=>'上链','type'=>'post','show'=>array('ispay'=>1,'isshlian'=>0)),
			
		),'batch'=>$batch,
		),
		'search'=>array(
			array('name'=>'ordersn','width'=>10,'title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'status','width'=>10,'title'=>'订单状态_Status','type'=>'radio','options'=>'status'),
			array('name'=>'buytype','width'=>10,'title'=>'订单类型_Type','type'=>'radio','options'=>'buytype'),
			array('name'=>'sendtype','width'=>10,'title'=>'配送方式_Ship type','type'=>'radio','options'=>'sendtype'),
			array('name'=>'merchid','width'=>10,'title'=>'所属商户_Merch','type'=>'select','option_value'=>'id','option_title'=>'merchname','options'=>'merchs'),
			array('type'=>'choose','width'=>13,'title'=>'所属会员','name'=>'mid','action'=>'core/member.get_member/'),
			array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range'),
		)
	);
	if($params['status']==1){
		$htmls['list']['list'][]=array('name'=>'send_status','title'=>'配送状态','type'=>'radio','options'=>'send_status');
	}else{
		$htmls['list']['list'][]=array('name'=>'status','title'=>'订单状态_status','type'=>'radio','options'=>'status');
	}
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	if($this->plugin_exists('jy_merch')){
		$merchs=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'),'id');
	}else{
		$merchs=array();
	}
	$merchs[0]=array('id'=>0,'merchname'=>'平台自营_Proprietary');
	foreach($merchs as &$mv){
		$mv['title']=$mv['merchname'];
	}
	$return['merchs']=$merchs;
	$return['esignok']=array('未签E签宝','已签E签宝');
	set_html('title',$shopset['title']);
	exi($return);
}

if($_RQ['op']=='uplian'){
	$id=$_RQ['id'];
	if(!$id){
		exi('请选择订单！','error');
	}
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$api_data=json_decode($order['api_data'],true);
	if(!$api_data['allqi']){
		exi('该商品不适合上链，不属于租赁订单！','error');
	}
	$this->model('member')->up_member($order['id']);
	$bizContent=$this->model('member')->orderinfo($order['ordersn']);
	$hash=$this->model('model')->biznotary($bizContent['hash']);
	$bizContent['leaseServiceFileHash']=$hash;
	$res=$this->model('model')->upinfo($bizContent,'','',true);
	$member=pdo_get('core_members',array('id'=>$order['mid']));
	if($member['isrealname']){
		$dunfen=array(
			'mobile'=>$member['mobile'],
			'user_name'=>$member['realname'],
			'cert_no'=>$member['cardnum'],
			'user_id'=>$order['mid'],
		);
		$res=model('jy_mayizlb/model')->risk_check($dunfen);
		$dunfen=$res['models'][0]['score'];
		$dundesc=$res['strategies'][0]['name'];
	}
	$api_data['dunfen']=$dunfen;
	$api_data['dundesc']=$dundesc;
	$api_data['liandata']=$res;
	$api_data['leaseServiceFileHash']=$hash;
	pdo_update('jy_weishop_order',array('api_data'=>ijson_encode($api_data)),array('id'=>$order['id']));
	$bizContent=$this->model('member')->ordergoodsinfo($order['id']);
	$res=$this->model('model')->upinfo($bizContent,'orderProduct','',true);
	$api_data['isshlian']=1;
	pdo_update('jy_weishop_order',array('api_data'=>ijson_encode($api_data)),array('id'=>$order['id']));
	exi('上链成功！');
}
?>